﻿using FirstFloor.ModernUI.Presentation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyInvoice.WPF.ViewModel
{
    public class MainWindowViewModel:NotifyPropertyChanged
    {
        
        private LinkGroupCollection menuLinkGroups;
        public LinkGroupCollection MenuLinkGroups
        {
            get { return this.menuLinkGroups; }
            set
            {
                this.menuLinkGroups = value;
                this.OnPropertyChanged("MenuLinkGroups");
            }
        }

        private LinkCollection titleLinks = new LinkCollection();

        public LinkCollection TitleLinks
        {
            get { return this.titleLinks; }
            set
            {
                this.titleLinks = value;
                this.OnPropertyChanged("TitleLinks");
            }
        }

        public string SelectedLinkUri
        {
            get { return this.SelectedLink.OriginalString; }
        }

        private Uri selectedLink = new Uri("/Pages/Introduction.xaml", UriKind.Relative);
        public Uri SelectedLink
        {
            get { return this.selectedLink; }
            set
            {
                if (value != null)
                {
                    this.selectedLink = value;
                    this.OnPropertyChanged("SelectedLink");
                }
            }
        }

        public MainWindowViewModel()
        {
            this.MenuLinkGroups = new LinkGroupCollection();
            this.RefreshMenus();
        }

        public void RefreshMenus()
        {
            this.CreateMainMenu();
            this.CreateTitleLinks();
        }

        private void CreateMainMenu()
        {
            this.MenuLinkGroups.Clear();

            #region Welcome Menu Link Group
            var welcomeLinkGroup = new LinkGroup { DisplayName = "Welcome", GroupName = "Main" };

            var welcomeIntroductionLink = new Link { DisplayName = "Introduction", Source = new Uri("/Pages/Introduction.xaml", UriKind.Relative) };
            welcomeLinkGroup.Links.Add(welcomeIntroductionLink);

            this.MenuLinkGroups.Add(welcomeLinkGroup);

            #endregion

            #region Help Menu Link Group

            var helpLinkGroup = new LinkGroup { DisplayName = "Help", GroupName = "Help" };

            var helpAboutLink = new Link { DisplayName = "About", Source = new Uri("/Pages/About.xaml", UriKind.Relative) };
            helpLinkGroup.Links.Add(helpAboutLink);

            var helpSupportLink = new Link { DisplayName = "Support", Source = new Uri("/Pages/Support.xaml", UriKind.Relative) };
            helpLinkGroup.Links.Add(helpSupportLink);

            this.MenuLinkGroups.Add(helpLinkGroup);

            #endregion

            #region Authenticaton Menu Link Group
            var authenticationLinkGroup = new LinkGroup { DisplayName = "Authentication", GroupName = "Authentication" };
            

            
            #endregion

            if (this.authenticatedUser != null)
            {

                #region Invoices Menu Link Group
                var invoicesLinkGroup = new LinkGroup { DisplayName = "Invoices", GroupName = "Main" };

                if (this.authenticatedUser.ViewInvoices)
                {
                    var invoicesListLink = new Link { DisplayName = "list", Source = new Uri("/Pages/InvoiceList.xaml", UriKind.Relative) };
                    invoicesLinkGroup.Links.Add(invoicesListLink);
                }

                if (this.authenticatedUser.AddInvoices)
                {
                    var invoicesBulkLink = new Link { DisplayName = "Create Bulk", Source = new Uri("/Pages/CreateInvoices.xaml", UriKind.Relative) };
                    invoicesLinkGroup.Links.Add(invoicesBulkLink);
                }
                this.MenuLinkGroups.Add(invoicesLinkGroup);

                #endregion

                #region Clients Menu Link Group

                var clientsLinkGroup = new LinkGroup { DisplayName = "clients", GroupName = "Main" };
                if (this.authenticatedUser.ViewClients)
                {
                    var clientsListLink = new Link { DisplayName = "list", Source = new Uri("/Pages/ClientList.xaml", UriKind.Relative) };
                    clientsLinkGroup.Links.Add(clientsListLink);
                }
                this.MenuLinkGroups.Add(clientsLinkGroup);

                #endregion

                #region Reports Menu Link Group

                var reportsLinkGroup = new LinkGroup { DisplayName = "reports", GroupName = "Main" };

                var reportsListLink = new Link { DisplayName = "list", Source = new Uri("/Pages/ReportsList.xaml", UriKind.Relative) };
                reportsLinkGroup.Links.Add(reportsListLink);

                var reportsDesignLink = new Link { DisplayName = "design", Source = new Uri("/Pages/ReportDesigner.xaml", UriKind.Relative) };
                reportsLinkGroup.Links.Add(reportsDesignLink);

                this.MenuLinkGroups.Add(reportsLinkGroup);
                #endregion

                #region Contracts Menu Link Group

                var contractsLinkGroup = new LinkGroup { DisplayName = "contracts", GroupName = "Main" };

                var contractsListLink = new Link { DisplayName = "list", Source = new Uri("/Pages/ContractsList.xaml", UriKind.Relative) };
                contractsLinkGroup.Links.Add(contractsListLink);

                this.MenuLinkGroups.Add(contractsLinkGroup);
                #endregion

                #region Settings Menu Link Group

                var settingsLinkGroup = new LinkGroup { DisplayName = "Settings", GroupName = "settings" };

                //var settingsDisplayLink = new Link { DisplayName = "Software", Source = new Uri("/Pages/Settings.xaml", UriKind.Relative) };
                //settingsLinkGroup.Links.Add(settingsDisplayLink);

                var appearanceTabLink = new Link { DisplayName = "Appearance", Source = new Uri("/Content/SettingsAppearance.xaml", UriKind.Relative) };
                settingsLinkGroup.Links.Add(appearanceTabLink);

                if (this.authenticatedUser.EditSettingsExport)
                {
                    var exportTabLin = new Link { DisplayName = "Export", Source = new Uri("/Content/SettingsExport.xaml", UriKind.Relative) };
                    settingsLinkGroup.Links.Add(exportTabLin);
                }
                if (this.authenticatedUser.EditSettingsEmail)
                {
                    var emailTabLink = new Link { DisplayName = "Email", Source = new Uri("/Content/SettingsEmail.xaml", UriKind.Relative) };
                    settingsLinkGroup.Links.Add(emailTabLink);
                }
                if (this.authenticatedUser.EditSettingsOptions)
                {
                    var settingsTabLink = new Link { DisplayName = "Options", Source = new Uri("/Content/SettingsOptions.xaml", UriKind.Relative) };
                    settingsLinkGroup.Links.Add(settingsTabLink);
                }
                if (this.authenticatedUser.EditSettingsDatabase)
                {
                    var databaseTabLink = new Link { DisplayName = "Database", Source = new Uri("/Content/SettingsDatabase.xaml", UriKind.Relative) };
                    settingsLinkGroup.Links.Add(databaseTabLink);
                }
                if (this.authenticatedUser.ViewUsers)
                {
                    var settingsUsersLink = new Link { DisplayName = "Users", Source = new Uri("/Pages/UserList.xaml", UriKind.Relative) };
                    settingsLinkGroup.Links.Add(settingsUsersLink);
                }
                this.MenuLinkGroups.Add(settingsLinkGroup);
                #endregion

                #region Log Out Menu Link Group
                var loginLink = new Link { DisplayName = "", Source = new Uri("/Content/LogIn.xaml", UriKind.Relative) };
                authenticationLinkGroup.Links.Add(loginLink);
                var logoutLink = new Link { DisplayName = "Logout", Source = new Uri("/Content/LogOut.xaml", UriKind.Relative) };
                authenticationLinkGroup.Links.Add(logoutLink);
                #endregion
            }
            else
            {
                #region Log In Menu Link Group

                var loginLink = new Link { DisplayName = "Login", Source = new Uri("/Content/LogIn.xaml", UriKind.Relative) };
                authenticationLinkGroup.Links.Add(loginLink);
                var logoutLink = new Link { DisplayName = "", Source = new Uri("/Content/LogOut.xaml", UriKind.Relative) };
                authenticationLinkGroup.Links.Add(logoutLink);
                
                #endregion
            }
            this.MenuLinkGroups.Add(authenticationLinkGroup);

            //this.SelectedLink = new Uri("/Pages/Introduction.xaml", UriKind.Relative);
        }

        private void CreateTitleLinks()
        {
            this.TitleLinks.Clear();
            //Title Links
            var mainTitleLink = new Link { DisplayName = "Main", Source = new Uri("/Pages/Introduction.xaml", UriKind.Relative) };
            this.TitleLinks.Add(mainTitleLink);

            if (this.authenticatedUser != null)
            {
               
                    var settingsTitleLink = new Link { DisplayName = "Settings", Source = new Uri("/Content/SettingsAppearance.xaml", UriKind.Relative) };
                
                this.TitleLinks.Add(settingsTitleLink);

            }
            if (this.authenticatedUser != null)
            {
                var loginTitleLink = new Link { DisplayName = string.Format("Logout {0}" , this.authenticatedUser.UserName), Source = new Uri("/Content/LogOut.xaml", UriKind.Relative) };
                this.TitleLinks.Add(loginTitleLink);
            }
            else
            {
                var loginTitleLink = new Link { DisplayName = "Login", Source = new Uri("/Content/LogIn.xaml", UriKind.Relative) };
                this.TitleLinks.Add(loginTitleLink);

            }
            var helpTitleLink = new Link { DisplayName = "Help", Source = new Uri("/Pages/About.xaml", UriKind.Relative) };
            this.TitleLinks.Add(helpTitleLink);
        }

        private UserViewModel authenticatedUser
        {
            get
            {
                if (EasyInvoice.WPF.Properties.Settings.Default.AuthenticatedUser != null)
                    return (UserViewModel)EasyInvoice.WPF.Properties.Settings.Default.AuthenticatedUser;
                return null; 
            }
        }
        
        
    }
}
